const app = getApp()
import global from './global'
import user from './user'
import {
    verifyPreOrder,
    createToken,
    setIsOpenSalary,
    queryRelations,
    openSalaryAndVerify
} from '../api/salary-card.js'

export function openSalaryCard(companyId, userName, userCard) {
    return new Promise((resolve, reject) => {
        createToken({
            openid: user.openid,
            userName: userName || user.userName,
            idCardNumber: userCard || user.cardNo,
            bankCardIs: 1,
            companyId
        }).then(res => {
            console.log(`/pages/card/auth?mchid=${res.mchid}&sub_mchid=${res.subMchid}&appid=${global.appId}&token=${res.token}&openid=${user.openid}`)
            wx.navigateToMiniProgram({
                appId: 'wxd24d9057cd83d47a',
                path: `/pages/card/auth?mchid=${res.mchid}&sub_mchid=${res.subMchid}&appid=${global.appId}&token=${res.token}&openid=${user.openid}`
            })
            resolve(res)
        }, err => {
            reject(err)
        })
    })
}

// 开通并核身
export function openSalaryVerify(companyId, companyName,projectId, projectName, userName, userCard) {
    return new Promise((resolve, reject) => {
        openSalaryAndVerify({
            projectName,
            employerName: companyName,
            projectId,
            openid: user.openid,
            userName: userName || user.userName,
            idCardNumber: userCard || user.cardNo,
            employmentType: 'SHORT_TERM_EMPLOYMENT',
            companyId,
        }).then(res => {
            wx.navigateToMiniProgram({
                appId: 'wxd24d9057cd83d47a',
                path: '/pages/card/authen_with_auth?mchid=' + res.mchid + '&sub_mchid=' + res.subMchid + '&openid=' + user.openid + '&token=' + res.token + '&authenticate_number=' + res.authenticateNumber + '&appid=' + global.appId
            })
            resolve(res)
        }, err => {
            reject(err)
        })
    })
}

export function verifyFace(data) {
    console.log(data, '我传递过来的数据')
    console.log(verifyFace)
    console.log(data, 'data信息')
    return new Promise((resolve, reject) => {
        data.authenticateType = data.authenticateType || 'SIGN_IN'
        app.confirm('即将打开"微工卡"小程序').then(() => {
            verifyPreOrder(data).then(res => {
                console.log(res, '返回值resres')
                console.log('/pages/card/authen_with_auth?mchid=' + res.mchid + '&sub_mchid=' + res.subMchid + '&openid=' + user.openid + '&token=' + res.token + '&authenticate_number=' + res.authenticateNumber + '&appid' + global.appId, '内容拼接结果')
                wx.navigateToMiniProgram({
                    appId: 'wxd24d9057cd83d47a',
                    path: '/pages/card/authen_with_auth?mchid=' + res.mchid + '&sub_mchid=' + res.subMchid + '&openid=' + user.openid + '&token=' + res.token + '&authenticate_number=' + res.authenticateNumber + '&appid=' + global.appId
                })
                resolve(res)
            }, err => {
                reject(err)
            })
        })
        
    })
}

export function goSalaryCard() {
    console.log(`/pages/card/card?mchid=1607959068&sub_mchid=${user.isOpenSalaryCard}&openid=${user.openid}&appid=${global.appId}`)
    wx.navigateToMiniProgram({
        appId: 'wxd24d9057cd83d47a',
        path: `/pages/card/card?mchid=1607959068&sub_mchid=${user.isOpenSalaryCard}&openid=${user.openid}&appid=${global.appId}`
    })
}

export function querySalaryCard(companyId) {
    // 查询微工卡
    return new Promise((resolve, reject) => {
        queryRelations({
            openid: user.openid,
            companyId
        }).then(res => {
            console.log(res, '查询开通微工啦')
            // 该员工是否开通微工卡
            if (res.registerState === undefined ? (res.authorizeState === 'AUTHORIZED') : (res.registerState === 'REGISTERED' && res.authorizeState === 'AUTHORIZED')) {
                console.log('判断员工是否开通微工卡')
                if (!user.isOpenSalaryCard && user.userId) {
                    user.isOpenSalaryCard = res.subMchid
                    setIsOpenSalary({
                        isOpenSalaryCard: res.subMchid
                    })
                }
                resolve(res)
            } else {
                console.log(res, 'resres')
                console.log('员工没有开通微工卡')
                if(res.isOpenSalaryCard){
                    console.log('else情况下 开通微工卡')
                    reject(true)
                }
                else{
                    console.log('else情况下 没开通微工卡')
                    // 如果该公司未授权微工卡，使用蓝豚互动
                    queryRelations({
                        openid: user.openid
                    }).then(res=>{
                        if (res.registerState === undefined ? (res.authorizeState === 'AUTHORIZED') : (res.registerState === 'REGISTERED' && res.authorizeState === 'AUTHORIZED')) {
                            if (!user.isOpenSalaryCard && user.userId) {
                                user.isOpenSalaryCard = res.subMchid
                                setIsOpenSalary({
                                    isOpenSalaryCard: res.subMchid
                                })
                            }
                            res.isOpenSalaryCard = false
                            resolve(res)
                        }
                        else{
                            reject(1)
                        }
                    })
                }
            }
        }, () => {
            reject(1)
        })
    })
}